package com.exampleyyy.demoyyy.common;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.exampleyyy.demoyyy.SHITIYYY.Zhifubao;
import org.springframework.stereotype.Component;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.response.AlipayTradeWapPayResponse;
@Component //  注册成spring的组件
public class Alipay {
    public String payPC(Zhifubao zhifubao) throws AlipayApiException {
        // 支付宝网关
        String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        // APPID，换成自己的引用ID
        String appId = "9021000122696147";
        // 商户私钥, 换成自己的私钥
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCcCXBNmwmkWSSK6it803BPW0aEQV3gXJqqIx5AA7lrufaUjj1A8mYfO9avoz6qaVADalxZPsWvYkZyFnfYsQ1DY89Uli2tqZs4wNzomFON0/OTM41HJ6uoyjCIdnC94X0HMBgeotLCUwDnj2qvaJaGiijBWRUC0r1ErDEzlriSoU/Jb3MAIxy+ukrkccJ/uBBhuoQaNCnLHWE3i0LwQTba1OOKnu34PsxsYaWd8XD8XVf6S2rchymgj1DogRbnoFBK7BX4MobQE411/OTKRWNfj/mnvwTf/pPiHadoFiSn3bNCNWWRq1W2MsQidG74z1RMvwgJ9HImD2UkzW3qNyTnAgMBAAECggEBAJX5oH6HCNJ1d+EpSFk3JRixQ2r1JRT0DRiAnp+qtLqtSCrrOEbHdY6JxkSU5DOI+4X84fxJkFqNW+ZmYBTfwD7InUvQ+OCVR4NaJDpPxudoFBKlFyKQJ7cZd2KCbMSlCbYx7MztdSOgAhuPRlyIaz2vZ5LHW6GWCqMJ2lSahpcvgrU/NEa+oYGG3QjOQj0RnjbMfLvXj9+HAwcfr6PAxCkf+pPYHBrY8Vgpz7QChypkW1woQ7YeDf4yiYbUf96SznCFP4Tqtmq5p7wNAjBdxW0Sd6xo0FlTNZGyBDttU/976eXZ+7jcfBbmjFRcx/jHIjqjeRvjgM7Oiq2XeCRUimECgYEAyh6c7fhXnmpLYtyQ0LStosSM7teK6Kkx3IEHo51p5jIqpJysGz7Zxo+kjf9MGghS7oxWn7q83mg5sE8u/wvU6qlddOoCqtqF6LP4vd8z1aaaXRFeEQPgHk1yh69OcbLUbKHA9kdDW75+5OXuSQe39bDMQQw66zFB8Mvt+ZKDU3ECgYEAxaH4nThTCZOVVojFAgUHCqoP+FVAyeFLJ+37VasrALgPJVB5oO5tL9Tdq6A2sc9dRnTBxxhF6hJJ3fX6NGy1XqKvJUZ5Ma1E98zeS1GQjHBa7ha9j8PmHpbzOIoOpd0N87ubfPZ7cpZzajY5aLZTCcr7qp1B5PDUnhKMLLCSodcCgYEAg6SOt5wBjWdT9G8OTzDuys3TsRhoHANfO2w6yoETYWAZUB/PJOu5sc8K8YVvTqFPSpnTLbPVodImiMP6KxnMso5fFW/5DsjHYPofL6O5whUCA031g4WPqozs5Jz0ra85FgwAfyeJIq8fPPzZR22kjnx5fIB3u4nYlfps5BleObECgYAFCHPxZQ7QTp7d/kuuGnysa9pOQ7WLrp4gXTIqY1qMASP5mkwOqiPDtRMUHNdnvdUtRynsLRUSufaBnZMKpL4Vs+Tr+Rh7CGOaP/BlUKVyviXCrHeVYrLIkqWNCg8HcFK7o0p5azmHmIdF7C2YGhSRSkcQeELGVs/DWsuRE8oYLwKBgQCOJQcKllla04Ybblc8KhILSlFmAdDpyaFbKqae7QQ/aw9JCcqTJmx1iCGTUkPEZ7pavlcf6UxsaTDFf9/TK+fVddFHKpOgF3ykctaKfXr2Zu31tlVu4qv4zvMl7mSTYIBxZMDM894cQzvRHbsI+pW2bn3Xck51PF/3SVnBqj29OA==";
        // 支付宝公钥, 换成自己的公钥，即对应APPID下的支付宝公钥
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs2cehcP7mLChUNHEgUfh17QEuxavXvJuK3VwP+cBYME7fy7ml+JggHMKfu2bJaI/tlC/X1TMGmvyIuMDXNzZnc9ohwTgiLQNzsuJwA7UMGRqFAbVDU7wmSpMqi8r1Z+9ljih2JoWqd1aVHvQ/GbFmcZ2oA16XzdGNaxjmtLGyg0CXPejg8Ml+0qzlqmuD4u+ffzi4XsBWqa0OJe2unV8r6Mr1+XiY8aXvAqmPWyCuoZ8z6gaRTGbYX4qjsgucwq355ToJwwaJZOd3VH+gqmz1+GX+cElO9e2WQzlpPz+P+c0UcKgSbTuF6TrNlLt92/krvEdxSYm9fbFCsvow/Q6HQIDAQAB";
        // 格式化为 json 格式
        String format = "json";
        // 字符编码格式
        String charset = "utf-8";
        // 签名方式或者叫加密方式
        String signType = "RSA2";
        // 服务器异步通知页面路径,通知到商家
        String notifyUrl = "";
        // 支付成功页面
        String returnUrl = "http://8.130.48.71:8081/front/page/pay-success.html";
        // 1、实例化支付客户端
        // 1、实例化支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        //7个参数:支付宝网关,商家支付宝id ,私钥, 格式json， 编码方式utf8 ,公钥，加密算法非对称加密算法
        // 2、实例化请求对象，请求支付宝二维码页面
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //3. 设置支付成功之后跳转的那个页面，也就是支付成功页面
        alipayRequest.setReturnUrl(returnUrl);
        // 4.通知商家，有人下单了
        alipayRequest.setNotifyUrl(notifyUrl);
        // 封装参数(以json格式封装)
        alipayRequest.setBizContent(JSON.toJSONString(zhifubao));
        // 5. 请求支付宝进行付款，并获取支付结果，查看是否支付成功
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // 返回付款信息
        return result;
    }

    public String pay(Zhifubao zhifubao) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
            // 创建配置对象
        // APPID，换成自己的引用ID
        String APPID = "9021000122696147";
        // 商户私钥, 换成自己的私钥
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCcCXBNmwmkWSSK6it803BPW0aEQV3gXJqqIx5AA7lrufaUjj1A8mYfO9avoz6qaVADalxZPsWvYkZyFnfYsQ1DY89Uli2tqZs4wNzomFON0/OTM41HJ6uoyjCIdnC94X0HMBgeotLCUwDnj2qvaJaGiijBWRUC0r1ErDEzlriSoU/Jb3MAIxy+ukrkccJ/uBBhuoQaNCnLHWE3i0LwQTba1OOKnu34PsxsYaWd8XD8XVf6S2rchymgj1DogRbnoFBK7BX4MobQE411/OTKRWNfj/mnvwTf/pPiHadoFiSn3bNCNWWRq1W2MsQidG74z1RMvwgJ9HImD2UkzW3qNyTnAgMBAAECggEBAJX5oH6HCNJ1d+EpSFk3JRixQ2r1JRT0DRiAnp+qtLqtSCrrOEbHdY6JxkSU5DOI+4X84fxJkFqNW+ZmYBTfwD7InUvQ+OCVR4NaJDpPxudoFBKlFyKQJ7cZd2KCbMSlCbYx7MztdSOgAhuPRlyIaz2vZ5LHW6GWCqMJ2lSahpcvgrU/NEa+oYGG3QjOQj0RnjbMfLvXj9+HAwcfr6PAxCkf+pPYHBrY8Vgpz7QChypkW1woQ7YeDf4yiYbUf96SznCFP4Tqtmq5p7wNAjBdxW0Sd6xo0FlTNZGyBDttU/976eXZ+7jcfBbmjFRcx/jHIjqjeRvjgM7Oiq2XeCRUimECgYEAyh6c7fhXnmpLYtyQ0LStosSM7teK6Kkx3IEHo51p5jIqpJysGz7Zxo+kjf9MGghS7oxWn7q83mg5sE8u/wvU6qlddOoCqtqF6LP4vd8z1aaaXRFeEQPgHk1yh69OcbLUbKHA9kdDW75+5OXuSQe39bDMQQw66zFB8Mvt+ZKDU3ECgYEAxaH4nThTCZOVVojFAgUHCqoP+FVAyeFLJ+37VasrALgPJVB5oO5tL9Tdq6A2sc9dRnTBxxhF6hJJ3fX6NGy1XqKvJUZ5Ma1E98zeS1GQjHBa7ha9j8PmHpbzOIoOpd0N87ubfPZ7cpZzajY5aLZTCcr7qp1B5PDUnhKMLLCSodcCgYEAg6SOt5wBjWdT9G8OTzDuys3TsRhoHANfO2w6yoETYWAZUB/PJOu5sc8K8YVvTqFPSpnTLbPVodImiMP6KxnMso5fFW/5DsjHYPofL6O5whUCA031g4WPqozs5Jz0ra85FgwAfyeJIq8fPPzZR22kjnx5fIB3u4nYlfps5BleObECgYAFCHPxZQ7QTp7d/kuuGnysa9pOQ7WLrp4gXTIqY1qMASP5mkwOqiPDtRMUHNdnvdUtRynsLRUSufaBnZMKpL4Vs+Tr+Rh7CGOaP/BlUKVyviXCrHeVYrLIkqWNCg8HcFK7o0p5azmHmIdF7C2YGhSRSkcQeELGVs/DWsuRE8oYLwKBgQCOJQcKllla04Ybblc8KhILSlFmAdDpyaFbKqae7QQ/aw9JCcqTJmx1iCGTUkPEZ7pavlcf6UxsaTDFf9/TK+fVddFHKpOgF3ykctaKfXr2Zu31tlVu4qv4zvMl7mSTYIBxZMDM894cQzvRHbsI+pW2bn3Xck51PF/3SVnBqj29OA==";
        // 支付宝公钥, 换成自己的公钥，即对应APPID下的支付宝公钥
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs2cehcP7mLChUNHEgUfh17QEuxavXvJuK3VwP+cBYME7fy7ml+JggHMKfu2bJaI/tlC/X1TMGmvyIuMDXNzZnc9ohwTgiLQNzsuJwA7UMGRqFAbVDU7wmSpMqi8r1Z+9ljih2JoWqd1aVHvQ/GbFmcZ2oA16XzdGNaxjmtLGyg0CXPejg8Ml+0qzlqmuD4u+ffzi4XsBWqa0OJe2unV8r6Mr1+XiY8aXvAqmPWyCuoZ8z6gaRTGbYX4qjsgucwq355ToJwwaJZOd3VH+gqmz1+GX+cElO9e2WQzlpPz+P+c0UcKgSbTuF6TrNlLt92/krvEdxSYm9fbFCsvow/Q6HQIDAQAB";

        String returnUrl = "http://8.130.48.71:8081/front/page/pay-success.html";
            // 支付成功后跳转的页面
            String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
            // 支付宝网关地址

            alipayConfig.setServerUrl(serverUrl);
            alipayConfig.setAppId(APPID);
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setAlipayPublicKey(alipayPublicKey);
            // 设置这些数据

            alipayConfig.setFormat("json");// 设置格式
            alipayConfig.setCharset("UTF-8");
            alipayConfig.setSignType("RSA2");

            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
            // 创建支付客户端对象
            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
            // 创建手机浏览器请求对象,用来请求打开支付宝app
            AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
            // 手机浏览器支付 ,拉起支付宝app

            model.setOutTradeNo(zhifubao.getOut_trade_no());// 订单编号
            model.setTotalAmount(zhifubao.getTotal_amount()); // 订单金额
            model.setSubject(zhifubao.getSubject());// 订单主题
            model.setProductCode("QUICK_WAP_WAY"); // 产品编号,手机端浏览器支付,固定,不需要修改
            model.setSellerId("hawqkq9129@sandbox.com"); // 设置成自己的商家支付宝账号
            // 注意:一定要用用商家的支付宝账号,在沙箱中的商家信息中
            request.setBizModel(model);//将数据设置到请求对象中
            request.setReturnUrl(returnUrl); // 设置支付成功之后跳转的页面

            AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
            // 发起支付请求
            String result = response.getBody();
            // 返回支付后的响应结果
            return result;
        }


}